<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             style="width: 90%;margin: auto"
             label-width="90px">
      <el-row>
        <el-col :span="9">
          <el-form-item label="字典类型">
            <el-input v-model="dataForm.dictType" placeholder="字典类型关键字"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="字典名称">
            <el-input v-model="dataForm.dictName" placeholder="字典类型名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-plus" class="btn_right" @click="addItem">新增</el-button>
        </el-col>
      </el-row>
      <el-row v-for="( item,index) in dataForm.dictData" :key="index">
        <el-col :span="9">
          <el-form-item label="字典key" prop="dictType">
            <el-input v-model="item.dictKey" placeholder="字典类型关键字"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="字典key值" prop="dictName">
            <el-input v-model="item.dictValue" placeholder="字典类型名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button icon="el-icon-delete" type="danger" @click="cancleItem(index)" class="btn_right">删除</el-button>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dynamicValidateForm: {
          domains: [{
            value: ''
          }],
          email: ''
        },
        visible: false,
        dataForm: {
          dictType: '',
          dictName: '',
          status: 0,
          dictData: []
        },
        dataRule: {
          dictType: [
            {required: true, message: '字典类型不能为空', trigger: 'blur'}
          ],
          dictName: [
            {required: true, message: '字典类型名称不能为空', trigger: 'blur'}
          ],
          dictKey: [
            {required: true, message: '字典key不能为空', trigger: 'blur'}
          ],
          dictValue: [
            {required: true, message: '字典值不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (dictType) {
        this.dataForm.dictType = dictType || null
        this.visible = true
        this.$nextTick(() => {
          this.dataForm = {
            dictType: '',
            dictName: '',
            status: 0,
            dictData: []
          }
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dictType !== null) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/info/${dictType}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.dict
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/${!this.dataForm.dictId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 删除单项key、值
      cancleItem (index) {
        this.dataForm.dictData.splice(index, 1)
      },
      // 添加key、值
      addItem () {
        this.dataForm.dictData.push({
          dictKey: '',
          dictValue: '',
          orderNum: 0,
          remark: ''
        })
      }
    }
  }
</script>
<style scoped="true">
  .btn_right {
    margin-left: 20px;
  }
</style>
